package org.game;

import static org.lwjgl.opengl.GL11.GL_FALSE;
import static org.lwjgl.opengl.GL20.*;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ShaderLoader {
  public static int VERTEX_SHADER = GL_VERTEX_SHADER;
  public static int FRAGMENT_SHADER = GL_FRAGMENT_SHADER;

	/** Load a pair of shaders as a program
	 * 
	 * @param vertex the location of the vertex shader
	 * @param fragment the location of the fragment shader
	 * @return
	 */
	public static int loadProgram(String vertex,String fragment){
		int programId = glCreateProgram();
		int vert = glCreateShader(GL_VERTEX_SHADER);
		int frag = glCreateShader(GL_FRAGMENT_SHADER);
		
		StringBuilder ver = new StringBuilder();
		StringBuilder fra = new StringBuilder();
		BufferedReader reader = null;
		try{
			reader = new BufferedReader(new FileReader(vertex));
			String line;
			while((line = reader.readLine()) != null){
				ver.append(line).append('\n');
			}
				reader.close();
				reader = new BufferedReader(new FileReader(fragment));
			while((line = reader.readLine()) != null){
				fra.append(line).append('\n');
			}
			reader.close();
		}catch(IOException e){
			e.printStackTrace();
		}
		
		glShaderSource(vert, ver);
		glCompileShader(vert);
		if(glGetShaderi(vert,GL_COMPILE_STATUS) == GL_FALSE){
			System.err.println("Vertex Shader error:");
			System.err.println(glGetShaderInfoLog(vert,1024));
		}
		glShaderSource(frag,fra);
		glCompileShader(frag);
		if(glGetShaderi(frag,GL_COMPILE_STATUS) == GL_FALSE){
			System.err.println("Fragment Shader error:");
			System.err.println(glGetShaderInfoLog(frag,1024));
		}
		glAttachShader(programId,vert);
		glAttachShader(programId,frag);
		glLinkProgram(programId);
		if(glGetShaderi(programId,GL_COMPILE_STATUS) == GL_FALSE){
			System.err.println("Program was unable to link correctly:");
			System.err.println(glGetShaderInfoLog(programId,1024));
		}
		glDeleteShader(vert);
		glDeleteShader(frag);
		return programId;
	}
   
     public static int loadShader(String path,int type){
       int shader;
       BufferedReader read = null;
       StringBuilder source = new StringBuilder;
        try{
          read = new BufferedReader(new FileReader(path));
          String line = null;
          while((line = read.readLine()) != null){
             source.append(line).append('\n');
            }
          read.close();
        }catch(IOException e){
          e.printStackTrace();
        }
  
        if(type == GL_VERTEX_SHADER){
         shader = glCreateShader(type);
        }else if(type == GL_FRAGMENT_SHADER){
         shader = glCreateShader(type);
        }else{
          return -1;
        }
        
        glShaderSource(shader, source);
        glCompileShader(shader);
        if(glGetShaderi(shader, GL_COMPILE_STATUS) != GL_FALSE){
          System.err.println("Shader error:"+(shader==GL_VERTEX_SHADER? "Vertex Shader":"Fragment Shader")+":");
          System.err.println(glGetShaderInfoLog(shader,1024));
        }
        
       
       return shader;
     }
}
